﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using qltscd.Models;
using qltscd.Views.DanhMucThietBi.Models;

namespace qltscd.Controllers
{ 
    public class ThayTheController : Controller
    {
        private QLTBEntities db = new QLTBEntities();

        public ActionResult Create()
        {
            ViewBag.ThietBiCu = new SelectList(db.DanhMucThietBis.OrderBy(i => i.MaThietBi), "IdentityThietBi", "SerialThietBi");
            ViewBag.ThietBiMoi = new SelectList(db.DanhMucThietBis.OrderBy(i => i.MaThietBi), "IdentityThietBi", "SerialThietBi");
            return View();
        }

        [HttpPost]
        public ActionResult Create(ThayThe thaythe)
        {
            if (ModelState.IsValid)
            {
                db.ThayThes.AddObject(thaythe);
                db.SaveChanges();
                ViewBag.ThietBiCu = new SelectList(db.DanhMucThietBis.OrderBy(i => i.MaThietBi), "IdentityThietBi", "SerialThietBi");
                ViewBag.ThietBiMoi = new SelectList(db.DanhMucThietBis.OrderBy(i => i.MaThietBi), "IdentityThietBi", "SerialThietBi");
                return RedirectToAction("Create");  
            }
            ViewBag.ThietBiCu = new SelectList(db.DanhMucThietBis.OrderBy(i => i.MaThietBi), "IdentityThietBi", "SerialThietBi", thaythe.ThietBiCu);
            ViewBag.ThietBiMoi = new SelectList(db.DanhMucThietBis.OrderBy(i => i.MaThietBi), "IdentityThietBi", "SerialThietBi", thaythe.ThietBiMoi);
            return View(thaythe);
        }

        private SelectList TaoDanhSachNguoiDung (int? selected) {
            List<DanhMucNguoiDung> List = db.DanhMucNguoiDungs.OrderBy(i => i.TenNguoiDung).ToList();
            List<SelectableNguoiDung> nds = new List<SelectableNguoiDung>();
            foreach (DanhMucNguoiDung nd in List) {
                nds.Add(new SelectableNguoiDung(nd.IdentityNguoiDung, nd.HoTenDem, nd.TenNguoiDung));
            }
            return new SelectList(nds, "IdentityNguoiDung", "HoTenNguoiDung", selected);
        }
    }
}